﻿
namespace Behemoth.SharePoint.Logging {

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Behemoth.SharePoint.Logging;

    /// <summary>
    /// Put this code in your class that produces log entries, or inherit 
    /// this class and create your own LOGGING_PRODUCT and LOGGING_CATEGORY.
    /// </summary>
    public abstract class LoggingEventConsumerBase {

        #region Logging Code Pattern v2.0

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        public event LoggingEventHandler Logging;

        protected abstract string LOGGING_PRODUCT {
            get; // "Behemoth";
        }
        protected abstract string LOGGING_CATEGORY {
            get; // "Logging Example";
        }

        /// <summary>
        /// Put a method like this in your class that produces log entries
        /// </summary>
        /// <param name="e"></param>
        protected virtual void Log(LoggingEventArgs e) {
            // if nobody could be bothered to attach their own events, we should
            // still log things in the default way
            if (Logging == null)
                Logging += new LoggingEventHandler(ULSTraceLogging.Default.Log);
            if (Logging != null)
                Logging(this, e);
        }
        protected virtual void Log(string msg) {
            Log(msg, TraceSeverity.InformationEvent);
        }
        protected virtual void Log(string msg, TraceSeverity severity) {
            LoggingEventArgs e = new LoggingEventArgs(LOGGING_CATEGORY, LOGGING_PRODUCT, msg, severity);
            Log(e);
        }
        protected virtual void Log(Exception ex) {
            LoggingEventArgs e = new LoggingEventArgs(LOGGING_CATEGORY, LOGGING_PRODUCT, ex);
            Log(e);
        }

        #endregion Logging Code Pattern

    } // class

    /* commented because it probably does not work the way we'd like it to
     * 
    /// <summary>
    /// Put this code in your class that produces log entries, or inherit 
    /// this class and create your own LOGGING_PRODUCT and LOGGING_CATEGORY.
    /// Use this class if you need to make logging calls from static methods,
    /// or when every instance of your class will be loggedd in the same way.
    /// </summary>
    public abstract class LoggingEventConsumerBaseStatic {

        #region Logging Code Pattern v2.0

        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        public static event LoggingEventHandler Logging;

        /// <summary>
        /// Use protected new static string to override.
        /// </summary>
        protected static string LOGGING_PRODUCT {
            get { return "Behemoth"; }
        }
        /// <summary>
        /// Use protected new static string to override.
        /// </summary>
        protected static string LOGGING_CATEGORY {
            get { return "Logging Example"; }
        }

        /// <summary>
        /// Put a method like this in your class that produces log entries
        /// </summary>
        /// <param name="e"></param>
        public static void Log(LoggingEventArgs e) {
            // if nobody could be bothered to attach their own events, we should
            // still log things in the default way
            if (Logging == null)
                Logging += new LoggingEventHandler(ULSTraceLogging.Default.Log);
            if (Logging != null)
                Logging(null, e);
        }
        public static void Log(string msg) {
            Log(msg, TraceSeverity.InformationEvent);
        }
        public static void Log(string msg, TraceSeverity severity) {
            LoggingEventArgs e = new LoggingEventArgs(LOGGING_CATEGORY, LOGGING_PRODUCT, msg, severity);
            Log(e);
        }
        public static void Log(Exception ex) {
            LoggingEventArgs e = new LoggingEventArgs(LOGGING_CATEGORY, LOGGING_PRODUCT, ex);
            Log(e);
        }

        #endregion Logging Code Pattern

    } // class
     */

} // namespace
namespace Behemoth.SharePoint.Logging.EventExample {

    // TODO turn this into a unit test class

    /// <summary>
    /// An example of one way you can consume these methods without a lot of code.
    /// </summary>
    public class ExampleLibraryClass : LoggingEventConsumerBase {

        protected override string LOGGING_PRODUCT {
            get { return "Behemoth"; } 
        }
        protected override string LOGGING_CATEGORY {
            get { return "Logging Example"; }
        }

    }

    /// <summary>
    /// Put this code into the class that creates your objects that have logging events
    /// </summary>
    public class ExampleLoggingEvent_CallingClass {

        /// <summary>
        /// Put this object in the class that creates the log and then
        /// calls your class that logs data via events.
        /// </summary>
        private static ULSTraceLogging uls = new ULSTraceLogging();
        // TODO Set default properties, if desired in your constructor, but events can have their own
        //uls.DefaultCategory = "";
        //uls.DefaultProduct = "";

        /// <summary>
        /// Put this code in the class that creates the log and then
        /// calls your class that logs data via events.
        /// </summary>
        public static void Main() {
            // don't instantiate the library class if it's a static one...
            LoggingEventConsumerBase library = new ExampleLibraryClass();
            library.Logging += new LoggingEventHandler(uls.Log);
            // ... Do some stuff
        }

    } // class
} //namespace
